package it.amicosmanettone.whatsimage.adapter;

import java.text.SimpleDateFormat;
import java.util.Date;

import it.amicosmanettone.whatsimage.R;
import it.amicosmanettone.whatsimage.SingleImageActivity;
import it.amicosmanettone.whatsimage.client.ApiClient;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
 
public class ImagesListAdapter extends ArrayAdapter {
	
	private Context mContext;
	private String categoryLabel;
	private String ORDER;
	
    public ImagesListAdapter(Context context, int textViewResourceId, String categoryLabel, String Order) {
		super(context, textViewResourceId);
		this.mContext = context;
		this.categoryLabel = categoryLabel;
		this.ORDER = Order;
	}
 
	@Override
    public View getView(final int position, View convertView, ViewGroup parent) {
    	LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		convertView = inflater.inflate(R.layout.row_images, null);
		
		final CircularImageView  imageCategory = (CircularImageView)convertView.findViewById(R.id.images_image);
		TextView textPremium = (TextView)convertView.findViewById(R.id.images_text_premium);
        
		Button buttonInfo = (Button)convertView.findViewById(R.id.images_button_info);
		
		buttonInfo.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				infoAlert(position);
			}
		});
		
		String urlImage = ApiClient.API_GetContentList(categoryLabel, ORDER).getImages().get(position).getPreview();
		String typeImage = ApiClient.API_GetContentList(categoryLabel, ORDER).getImages().get(position).getType();

		
		boolean downloadImage = ApiClient.getImage(imageCategory, urlImage);
		
        Log.v("ImageDownloader", "Finito scaricamento immagine");
        
        if(!downloadImage){
        
        	Log.v("IMM","SETTO SE NON PRESENTE");
        	Drawable res = mContext.getResources().getDrawable(R.drawable.no_image);
        	imageCategory.setImageDrawable(res);
        	
        }else if(downloadImage){
        	
        	if(!typeImage.equals("PREMIUM")){
        		textPremium.setVisibility(View.INVISIBLE);
            }
        	
        }
        
        imageCategory.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {

					Intent singleImage = new Intent(mContext.getApplicationContext(),
							SingleImageActivity.class);
					
					singleImage.putExtra("position", position);
					singleImage.putExtra("categoryLabel", categoryLabel);
					singleImage.putExtra("ORDER", ORDER);
					mContext.startActivity(singleImage);
					//overridePendingTransition(mContext.getResources().getAnimation(R.xml.translate_in),mContext.getResources().getAnimation(R.xml.translate_out));

			}

		});
        
        return convertView;
    }
	
	public void infoAlert(int alertPosition){
		
		String title = mContext.getString(R.string.images_button_info).toString();
		String date = mContext.getString(R.string.images_alert_date).toString();
		String desc = mContext.getString(R.string.images_alert_desc).toString();
		String author = mContext.getString(R.string.images_alert_author).toString();
		String buttonOk = mContext.getString(R.string.alert_button_ok).toString();
		
		String authorImage = ApiClient.API_GetContentList(categoryLabel, ORDER).getImages().get(alertPosition).getSource();
		String descImage = ApiClient.API_GetContentList(categoryLabel, ORDER).getImages().get(alertPosition).getDescription();
		final float RATING = ApiClient.API_GetContentList(categoryLabel, ORDER).getImages().get(alertPosition).getRating();
		String typeImage = ApiClient.API_GetContentList(categoryLabel, ORDER).getImages().get(alertPosition).getType();
		
		SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
		String dateImage = formatter.format(new Date(ApiClient.API_GetContentList(categoryLabel, ORDER).getImages().get(alertPosition).getCreationDate()));
		
		AlertDialog.Builder alert = new AlertDialog.Builder(mContext);
		alert.setTitle(title);
		
		TextView message = new TextView(mContext);
		message.setTextSize(15);
		message.setPadding(10, 8, 10, 8);
		message.setText("\n\r" + date + " " + dateImage + "\n\r" + desc + " " + descImage + "\n\r" + author + " " + authorImage + "\n\r");
		
		alert.setView(message);
		alert.setCancelable(false);

		alert.setNeutralButton(buttonOk, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				
				
				

			}// End onClick
			
		});// End of voteButton

		AlertDialog alertDialog = alert.create();
		alertDialog.show();
		
	}
}